from django.shortcuts import render, render_to_response
from django.http.response import HttpResponse, HttpResponseRedirect
from django.http import JsonResponse
import json
from publish.models import *
from publish.forms import *


# Create your views here.

def project_list(request):
    header_title, sub_title, path1, path2 = "", "", "发布管理", "项目配置"
    return render(request, 'publish/project_lsit.html', locals())


def project_getlist(request):
    offset = request.GET.get('offset', None)
    limit = request.GET.get('limit', None)
    sort = request.GET.get('sort', None)
    order = request.GET.get('order', None)
    search = request.GET.get('search', None)
    if sort is None:
        sort = "name"
    if order == "desc":
        sort = "-name"
    count = Project.objects.all().count()
    pro_list = Project.objects.order_by(sort).all()[int(offset):int(offset + limit)]
    if search is not None:
        pro_list = Project.objects.order_by(sort).filter(name__icontains=search)[int(offset):int(offset + limit)]
    rows = [{
        'id': p.id,
        'name': p.name,
        'audit': p.audit,
        'status': p.status
    } for p in pro_list]
    data = {'total': count, 'rows': rows}
    return JsonResponse(data)


def project_create(request):
    header_title, sub_title, path1, path2 = "", "", "发布管理", "配置项目"
    project_form = ProjectForm()
    if request.method == 'POST':
        pass
    return render(request, 'publish/project_create.html', locals())


def task_list(request):
    header_title, sub_title, path1, path2 = "", "", "发布管理", "上线单列表"
    return render(request, 'publish/task_list.html', locals())


def task_submit(request):
    header_title, sub_title, path1, path2 = "", "", "发布管理", "创建上线单"
    return render(request, 'publish/task_submit.html', locals())


def task_deploy(request):
    header_title, sub_title, path1, path2 = "", "", "发布管理", "部署上线"
    return render(request, 'publish/task_deploy.html', locals())


def record_list(request):
    header_title, sub_title, path1, path2 = "", "", "发布管理", "项目配置"
    return render(request, 'publish/record_list.html', locals())
